import sys
from gmssl import sm2, func
from base64 import b64encode, b64decode
# sm2的公私钥
from pysmx.SM3._SM3 import str2bytes, Hash_sm3

# SM2_PRIVATE_KEY = '0cfa22d991719d8c09141009b87d0c33e4421b6b8cdec8907e3ee0f4f4f33dc0'
# publick = "3a1e70cbb720838908afba11b66a9268d44bf0a5271984ad2abe22c39cf3626efd9468a9c5d73c190c5012ae9fb7d8e7e6f949bf8f749b71731c7eab9a1d686c"
# SM2_PUBLIC_KEY = 'o4f88c02450feb95a5d3be4c5bb85d1cce63f0f52181af963335a451cedb1c34c13093dd6aca4908c25e5a49577ff1c720196348556a6dcf15dbc8b073aa6df88a'
# new_SM2_PUBLIC_KEY = SM2_PUBLIC_KEY[2:]
# SM2_PUBLIC_KEY = 'dc89d3cf847090445bd6af066ece9443150fc6797d51e2fe72e10cf9a0d81ed30670ed54ad3bf6587754ca7dfea12eb16264904a3fd8ca413cdd9630752dfd2f'
class Sm2_sign():
    def __init__(self, SM2_PRIVATE_KEY, SM2_PUBLIC_KEY):

        self.sm2_crypt = sm2.CryptSM2(private_key=SM2_PRIVATE_KEY, public_key=SM2_PUBLIC_KEY)

    # 加密
    def encrypt(self, info):
        encode_info = self.sm2_crypt.encrypt(info.encode(encoding="utf-8"))
        encode_info = b64encode(encode_info).decode()  # 将二进制bytes通过base64编码
        return encode_info

    def sm2_hash_e(self, data, id, idlen, publickey):
        a = b'\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF' \
            b'\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC'

        b = b'\x28\xE9\xFA\x9E\x9D\x9F\x5E\x34\x4D\x5A\x9E\x4B\xCF\x65\x09\xA7' \
            b'\xF3\x97\x89\xF5\x15\xAB\x8F\x92\xDD\xBC\xBD\x41\x4D\x94\x0E\x93'

        gx = b'\x32\xc4\xae\x2c\x1f\x19\x81\x19\x5f\x99\x04\x46\x6a\x39\xc9\x94' \
             b'\x8f\xe3\x0b\xbf\xf2\x66\x0b\xe1\x71\x5a\x45\x89\x33\x4c\x74\xc7'

        gy = b'\xbc\x37\x36\xa2\xf4\xf6\x77\x9c\x59\xbd\xce\xe3\x6b\x69\x21\x53' \
             b'\xd0\xa9\x87\x7c\xc6\x2a\x47\x40\x02\xdf\x32\xe5\x21\x39\xf0\xa0'
        pubkey_xtemp = publickey[0:64]
        pubkey_ytemp = publickey[64:128]

        pubkey_x = str2bytes(pubkey_xtemp, 64)
        pubkey_y = str2bytes(pubkey_ytemp, 64)

        if idlen == 0:
            entl = b'\x00\x00'

            Zatemp = entl + a + b + gx + gy + pubkey_x + pubkey_y
        else:
            lentemp = 4 * idlen
            entl = int.to_bytes(lentemp, 2, byteorder='big', signed=False)
            idtemp = str2bytes(id, idlen)
            Zatemp = entl + idtemp + a + b + gx + gy + pubkey_x + pubkey_y
        Zatemp_v2 = byte2hex(Zatemp)
        etemp = Zatemp + data
        e = Hash_sm3(etemp, 1)
        resbytes = str2bytes(e, 64)

    # 解密
    def decrypt(self, info):
        decode_info = b64decode(info.encode())  # 通过base64解码成二进制bytes
        decode_info = self.sm2_crypt.decrypt(decode_info).decode(encoding="utf-8")
        return decode_info

    def make_file_java_byte_array_compatible(self, file_obj):
        """
        Reads in a file and converts it to a format accepted as Java byte array
        :param file object
        :return string
        """
        encoded_data = b64encode(file_obj.encode("utf-8"))
        print(encoded_data)
        strg = [x for x in bytearray('1576059389832','utf_8')]
        return strg

    # 签名
    def sign(self, data):
        # data_info = data.encode('utf-8')  # bytes类型
        random_hex_str = func.random_hex(self.sm2_crypt.para_len)
        sign = self.sm2_crypt.sign(data, random_hex_str)  # 16进制
        return sign

    # 验签
    def verify_sign(self, sign_data, data):
        result = self.sm2_crypt.verify(sign_data, data)
        return result

if __name__ == "__main__":
    import time

    SM2_PRIVATE_KEY = '0cfa22d991719d8c09141009b87d0c33e4421b6b8cdec8907e3ee0f4f4f33dc0'
    # timeArray = time.localtime(time.time())
    # data_time = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    sm2_t = Sm2_sign(SM2_PRIVATE_KEY)
    # print(sm2_t.sign(data_time))
    # # encrypted_contact_info = encrypt(contact_info)
    # # print(encrypted_contact_info)
    # # decrypted_contact_info = decrypt(encrypted_contact_info)
    # # print(decrypted_contact_info)
    # # sign(contact_info)
    sm2_t.make_file_java_byte_array_compatible("111111111111111111111")
